package com.cskaoyan.service;

import com.alibaba.excel.EasyExcel;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.StorageExample;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.bean.sysmanage.KeywordExcel;
import com.cskaoyan.bean.sysmanage.StorageVo;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.exception.MarketException;
import com.cskaoyan.listener.KeywordExcelListener;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.utils.MarketPageUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    StorageMapper storageMapper;

    @Override
    public BasePageData queryStorage(String key, String name, BaseParam param) {
        MarketPageUtil.startPage(param);
        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if(!StringUtils.isEmpty(key)){
            criteria.andKeyEqualTo(key);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameEqualTo(name);
        }
        storageExample.setOrderByClause(param.getSort() + " "+ param.getOrder());
        List<Storage> storages = storageMapper.selectByExample(storageExample);
        return BasePageData.ok(storages);
    }

    @Override
    public StorageVo updateStorageName(StorageVo storageVo) {
        Storage storage = new Storage();
        storage.setId(storageVo.getId());
        storage.setName(storageVo.getName());
        storage.setUpdateTime(new Date());
        storageMapper.updateByPrimaryKeySelective(storage);
        Storage updateStorage = storageMapper.selectByPrimaryKey(storageVo.getId());
        StorageVo updateStorageVo = new StorageVo();
        updateStorageVo.setId(updateStorage.getId());
        updateStorageVo.setKey(updateStorage.getKey());
        updateStorageVo.setName(updateStorage.getName());
        updateStorageVo.setType(updateStorage.getType());
        updateStorageVo.setSize(updateStorage.getSize());
        updateStorageVo.setUrl(updateStorage.getUrl());
        updateStorageVo.setAddTime(updateStorage.getAddTime());
        updateStorageVo.setUpdateTime(updateStorage.getUpdateTime());
        return updateStorageVo;
    }

    @Override
    public boolean deleteStorage(StorageVo storageVo) {
        int deleteRow = storageMapper.deleteByPrimaryKey(storageVo.getId());
        return deleteRow == 1;
    }

//    @SneakyThrows
//    @Override
//    public void importKeywordData(MultipartFile file, KeyWordService keyWordService) {
//        try {
//            InputStream inputStream = file.getInputStream();
//            EasyExcel.read(inputStream, KeywordExcel.class,new KeywordExcelListener(keyWordService)).sheet().doRead();
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new MarketException("文件读取失败");
//        }
//    }
}
